﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;

namespace Enterprise.Service.GridViewHelper
{
    /// <summary>
    /// Gridview助手
    /// </summary>
    public class GvHelper
    {
        //public GridView gv;

        /// <summary>
        /// 数据集
        /// </summary>
        public DataSet dataset;

        /// <summary>
        /// 构造函数
        /// </summary>
        public GvHelper() { }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="strWhere">条件，字符串不用写Where</param>
        /// <param name="gv">GridView</param>
        public void BindData(string tableName, string strWhere,GridView gv)
        {           
            Data.Common.Data cdata = new Data.Common.Data();
            string sql = "select t.*,t.rowid from " + tableName+" t";
            if (!string.IsNullOrEmpty(strWhere))
            {
                sql += " where "+strWhere;
            }
            dataset = cdata.GetDataSet(sql);
            gv.DataSource = dataset;
            gv.DataBind();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="gv"></param>
        public void BindData(string sql, GridView gv)
        {
            Data.Common.Data cdata = new Data.Common.Data();
            dataset = cdata.GetDataSet(sql);
            gv.DataSource = dataset;
            gv.DataBind();
        }
    }
}
